﻿using AppDataStruct;
using ComUIControl;
using KCommon;
using KWindow.KPageCore;
using LogLib;
using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace YSLPages
{
    public partial class PageSelectDocumentType : PageControlBase
    {
        private Image imgHZ = null;

        private Image imgHZCheck = null;

        private Image imgSCZ = null;

        private Image imgSCZCheck = null;

        private Image imgDLZ = null;

        private Image imgDLZCheck = null;

        private Button buttonTWZ = null;

        private Button buttonPassport = null;

        private Button buttonGAZ = null;

        private PreApplyInfo _appInfo = null;

        private bool HZChecked = false;

        private bool SCZChecked = false;

        private bool DLZChecked = false;

        public PageSelectDocumentType()
        {
            InitializeComponent();
            SetProcessBase(ProcessSelectDocumentType.Instance);
        }

        private void GetControls()
        {
            imgHZ = GetImage("HZSQ");
            imgHZCheck = GetImage("HZSQ_Check");
            imgSCZ = GetImage("SCZSQ");
            imgSCZCheck = GetImage("SCZQ_Check");
            imgDLZ = GetImage("DLZSQ");
            imgDLZCheck = GetImage("DLZSQ_Check");
            buttonTWZ = GetControl<Button>("buttonTWZ") as Button;
            buttonGAZ = GetControl<Button>("buttonGAZ") as Button;
            buttonPassport = GetControl<Button>("buttonPassport") as Button;
            //buttonGAZ = GetControl<Button>("buttonGAZ") as Button;
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            base.InitCtrls();
            GetControls();
            SetButtonDefaultStyles(buttonTWZ);
            SetButtonDefaultStyles(buttonPassport);
            SetButtonDefaultStyles(buttonGAZ);
        }

        private void InitButtonImage()
        {
            buttonPassport.BackgroundImage = HZChecked ? imgHZCheck : imgHZ;
            buttonTWZ.BackgroundImage = DLZChecked ? imgDLZCheck : imgDLZ;
            buttonGAZ.BackgroundImage = SCZChecked ? imgSCZCheck : imgSCZ;
        }

        //初始化窗体数据
        public override void InitData()
        {
            base.InitData();
            _appInfo = Process.GetValue<PreApplyInfo>("PreApplyInfo");
            Log.WriteAsync($"apply info:{JsonHandler.ConvertToJsonString<PreApplyInfo>(_appInfo)}");
            if (Process.ContainsKey("HZChecked"))
            {
                HZChecked = Process.GetValue<bool>("HZChecked");
            }
            else
            {
                HZChecked = false;
            }
            if (Process.ContainsKey("DLZChecked"))
            {
                DLZChecked = Process.GetValue<bool>("DLZChecked");
            }
            else
            {
                DLZChecked = false;
            }
            if (Process.ContainsKey("SCZChecked"))
            {
                SCZChecked = Process.GetValue<bool>("SCZChecked");
            }
            else
            {
                SCZChecked = false;
            }
            InitButtonImage();
            if (!SCZChecked && !HZChecked && !DLZChecked)
            {
                Speak("请选择您需要办理的证件类型");
            }
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            UpdateSQZJChecked();
            Process.SetValue("SCZChecked", SCZChecked);
            Process.SetValue("HZChecked", HZChecked);
            Process.SetValue("DLZChecked", DLZChecked);
            Process.SetValue("PreApplyInfo", _appInfo);
        }

        private void UpdateSQZJChecked()
        {
            if (buttonTWZ.BackgroundImage == imgDLZCheck)
            {
                DLZChecked = true;
            }
            else
            {
                DLZChecked = false;
            }

            if (buttonPassport.BackgroundImage == imgHZCheck)
            {
                HZChecked = true;
            }
            else
            {
                HZChecked = false;
            }

            if (buttonGAZ.BackgroundImage == imgSCZCheck)
            {
                SCZChecked = true;
            }
            else
            {
                SCZChecked = false;
            }

        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            InitButtonImage();
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        public override void StartWork()
        {
            base.StartWork();
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            Log.WriteAsync("用户操作超时，返回首页");
            base.OnCountdownCompleted(sender, e);
        }

        protected override void btnBack_Click(object sender, EventArgs e)
        {
            //base.btnBack_Click(sender, e);
            StopCountdown();
            ClearData();
            //Process.ToAny("YSLPages.PageLivePhoto");
            base.btnBack_Click(sender, e);
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            UpdateSQZJChecked();
            if (!HZChecked && !SCZChecked && !DLZChecked)
            {
                MsgBox.Show("没有选择任一申请类型，请选择申请证件类型.");
                return;
            }
            StopCountdown();
            UpdateData();
            ClearData();

            //if (HZChecked && _config.WorkFlow.Signature)
            //{
            //    Process.ToAny("YSLPages.PageSignature");
            //}
            //else if (_config.WorkFlow.CapFinger)
            //{
            //    Process.ToAny("YSLPages.PageCaptureFinger");
            //}
            //else if (_config.WorkFlow.ReceiveWay)
            //{
            //    Process.ToAny("YSLPages.PageSelectReceiveWay");
            //}
            //else
            //{
            //    Process.ToAny("YSLPages.PagePreviewChecking");
            //}

            //跳转到采集现场人像
            Process.ToAny("YSLPages.PageLivePhoto");
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            StopCountdown();
            base.btnCancel_Click(sender, e);
        }

        private void btnTWZ_Click(object sender, EventArgs e)
        {
            if (buttonTWZ.BackgroundImage == imgDLZ)
            {
                StopCountdown();
                buttonTWZ.BackgroundImage = imgDLZCheck;
                UpdateData();
                Process.ToAny("YSLPages.PageApplyTWZInfo");
            }
            else
            {
                if (_appInfo != null)
                {
                    _appInfo.DLZSQ.Reset();
                }
                buttonTWZ.BackgroundImage = imgDLZ;
            }
        }

        private void btnSCZ_Click(object sender, EventArgs e)
        {
            if (buttonGAZ.BackgroundImage == imgSCZ)
            {
                StopCountdown();
                buttonGAZ.BackgroundImage = imgSCZCheck;
                UpdateData();
                Process.ToAny("YSLPages.PageApplyGAZInfo");
            }
            else
            {
                buttonGAZ.BackgroundImage = imgSCZ;

            }
        }

        private void btnHZ_Click(object sender, EventArgs e)
        {
            if (buttonPassport.BackgroundImage == imgHZ)
            {
                StopCountdown();
                buttonPassport.BackgroundImage = imgHZCheck;
                UpdateData();
                Process.ToAny("YSLPages.PageApplyPassportInfo");
            }
            else
            {
                buttonPassport.BackgroundImage = imgHZ;
            }
        }
    }
}
